package com.pn.controller;

import com.pn.dto.AssignRoleDto;
import com.pn.entity.Auth;
import com.pn.entity.Role;
import com.pn.entity.User;
import com.pn.entity.UserPageList;
import com.pn.service.RoleService;
import com.pn.service.UserService;
import com.pn.utils.PageBean;
import com.pn.utils.Result;
import com.pn.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RequestMapping("/user")
@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;

    //加载用户权限下的菜单树
    @GetMapping("/auth-list")
    public Result loadAuthTree(){
        Map<String,Object> map=(Map<String, Object>) ThreadLocalUtil.get();
        Integer userId =(Integer) map.get("userId");
        List<Auth> list = userService.autoTreeByUid(userId);
        return Result.ok(list);
    }
    //用户分页查询
    @GetMapping("/user-list")
    public Result<PageBean<User>> userList(UserPageList userPageList){
        PageBean<User> userList = userService.userList(userPageList);
       return Result.ok(userList);
    }

    //添加用户
    @PostMapping("/addUser")
    public Result addUser(@RequestBody User user){
         userService.addUser(user);
         return Result.ok("用户添加成功");
    }

    //用户的启用禁用
    @PutMapping("/updateState")
    public Result updateState(@RequestBody User user){
        int i = userService.updateState(user);
        if(i > 0){
            return Result.ok("启用禁用成功!");
        }
        return Result.err(Result.CODE_ERR_BUSINESS,"启用禁用失败");
    }

    //查询用户已分配的角色
    @GetMapping("/user-role-list/{userId}")
    public Result userRoleList(@PathVariable("userId") Integer userId){
        List<Role> roleList = roleService.userRoleList(userId);
        return Result.ok(roleList);
    }

    //给用户分配对象
    @PutMapping("assignRole")
    public Result userAssignRole(@RequestBody AssignRoleDto assignRoleDto){
        userService.assignRole(assignRoleDto);
        return Result.ok("分配角色成功");
    }

   //根据userId删除单个用户
    @DeleteMapping("deleteUser/{userId}")
    public Result deleteUser(@PathVariable Integer userId){

        Result result = userService.deleteUserByIds(Arrays.asList(userId));
        return result;
    }


    //根据userId删除多个用户
    @DeleteMapping("/deleteUserList")
    public Result deleteUserByIds(@RequestBody List<Integer> userIds){
        Result result = userService.deleteUserByIds(userIds);
        return result;
    }

    //修改用户
    @PutMapping("/updateUser")
    public Result updateUser(@RequestBody User user){
       Result result = userService.updateUserByUid(user);
        return result;
    }

    //重置密码
    @PutMapping("/updatePwd/{userId}")
    public Result updatePwd(@PathVariable Integer userId){
        Result result = userService.updatePwdByUid(userId);
        return result;
    }

}
